package com.ruoyi.common.utils.excl.workbook;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.ruoyi.common.utils.excl.annotation.Required;
import org.apache.poi.ss.usermodel.*;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * WorkbookExcelUtils
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/4/7 12:06
 */
public class SheetUtils {

    /**
     * 冻结白表格信息
     *
     * @param workbook   excelWorkbook
     * @param sheetIndex 表格下摆哦
     * @param colSplit   列数
     * @param rowSplit   行数
     */
    public static void createFreezePane(Workbook workbook, int sheetIndex, int colSplit, int rowSplit) {
        Sheet sheetAt = workbook.getSheetAt(sheetIndex);
        sheetAt.createFreezePane(colSplit, rowSplit);
    }

    /**
     * 修改设置 excel中表头必填字段信息
     *
     * @param workbook    workbook模板信息
     * @param sheet       表格序号
     * @param rowNum      列号
     * @param entityClass 实体类信息
     */
    public static void updateRequiredHeaderStyle(Workbook workbook, Sheet sheet, int rowNum, Class<?> entityClass) {
        Field[] fields =
                entityClass.getDeclaredFields();
        List<Integer> requiredOrderNumList = new ArrayList<>();
        for (Field field : fields) {
            Required required = field.getAnnotation(Required.class);
            if (required == null) {
                continue;
            }
            Excel excel = field.getAnnotation(Excel.class);
            String orderNum = excel.orderNum();
            requiredOrderNumList.add(Integer.parseInt(orderNum));
        }

        if (requiredOrderNumList.size() == 0) {
            return;
        }

        Row row = sheet.getRow(rowNum);
        Cell cell;
        RichTextString richStringCellValue;
        // 标红
        Font font = FontUtils.createFont(workbook, true, Font.COLOR_RED);
        for (Integer orderNum : requiredOrderNumList) {
            cell = row.getCell(orderNum);
            richStringCellValue = cell.getRichStringCellValue();
            richStringCellValue.applyFont(font);
            cell.setCellValue(richStringCellValue);
        }
    }

    /**
     * 获取第一张数据库表信息
     *
     * @param workbook workbook实体
     * @return 表格信息
     */
    public static Sheet getFirstSheet(Workbook workbook) {
        return workbook.getSheetAt(0);
    }

    /**
     * 自动列宽识别
     *
     * @param workbook    excelWorkbook
     * @param sheetIndex  表格下标
     * @param columAmount 表格列数
     */
    public static void autoSizeColumn(Workbook workbook, int sheetIndex, int columAmount) {
        autoSizeColumn(workbook, sheetIndex, 0, columAmount);
    }

    /**
     * 自动列宽识别
     *
     * @param workbook    excelWorkbook
     * @param sheetIndex  表格下标
     * @param startAmount 开始列数（从0开始）
     * @param endAmount   结束列数（从0开始）
     */
    public static void autoSizeColumn(Workbook workbook, int sheetIndex, int startAmount, int endAmount) {
        Sheet sheet = workbook.getSheetAt(sheetIndex);
        for (int i = startAmount; i < endAmount; i++) {
            sheet.autoSizeColumn(i);
        }
    }
}
